package com.tengju.user.domain.model.sentiment;

import com.tengju.user.domain.model.user.SsoUserId;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.*;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Value;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Value
public class Sentiment {

    List<FormComponent> formComponentList;

    SsoUserId ssoUserId;

    String operator;

    UserIdCode idCode;

    LocalDateTime createTime;

    public Sentiment(List<FormComponent> formComponentList, SsoUserId ssoUserId, String operator, UserIdCode idCode, LocalDateTime createTime) {
        if (CollectionUtils.isEmpty(formComponentList)) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,"客情内容为空");
        }
        if (ssoUserId == null || StringUtils.isEmpty(operator)) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,"操作人为空");
        }
        if (idCode == null) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,"用户id为空");
        }
        if (createTime == null) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,"创建时间为空");
        }
        this.formComponentList = formComponentList;
        this.ssoUserId = ssoUserId;
        this.operator = operator;
        this.idCode = idCode;
        this.createTime = createTime;
    }

    public String getContent() {
        if (CollectionUtils.isEmpty(formComponentList)) {
            return "";
        }
        for (FormComponent formComponent : formComponentList) {
            if ("talkContent".equals(formComponent.getCode()) && FormComponentType.html.equals(formComponent.getType())) {
                return HtmlConvertUtil.delHtmlTagAndDealImgAndVideo(CastUtil.castString(formComponent.getFilledData()));
            }
        }
        return "";
    }
}
